package top.maof.book.service.myImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.maof.book.dao.ShelfMapper;
import top.maof.book.model.Book;
import top.maof.book.model.Score;
import top.maof.book.model.Shelf;
import top.maof.book.result.PageData;
import top.maof.book.result.PageWrap;
import top.maof.book.service.ShelfService;
import top.maof.book.utils.Utils;

import java.util.List;

@Service
public class ShelfServiceImpl extends ServiceImpl<ShelfMapper, Shelf> implements ShelfService {

    @Autowired
    private ShelfMapper shelfMapper;


    @Override
    public PageData<Shelf> findPage(PageWrap<Shelf> pageWrap) {
        IPage<Shelf> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        QueryWrapper<Shelf> queryWrapper = new QueryWrapper<>();
        Utils.MP.blankToNull(pageWrap.getModel());
        if (pageWrap.getModel().getId() != null) {
            queryWrapper.lambda().eq(Shelf::getId, pageWrap.getModel().getId());
        }
        if (pageWrap.getModel().getBookId() != null) {
            queryWrapper.lambda().eq(Shelf::getBookId, pageWrap.getModel().getBookId());
        }
        if (pageWrap.getModel().getUserId() != null) {
            queryWrapper.lambda().eq(Shelf::getUserId, pageWrap.getModel().getUserId());
        }
        if (pageWrap.getModel().getInsertDate() != null) {
            queryWrapper.lambda().ge(Shelf::getInsertDate, Utils.Date.getStart(pageWrap.getModel().getInsertDate()));
            queryWrapper.lambda().le(Shelf::getInsertDate, Utils.Date.getEnd(pageWrap.getModel().getInsertDate()));
        }
        for(PageWrap.SortData sortData: pageWrap.getSorts()) {
            if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) {
                queryWrapper.orderByDesc(sortData.getProperty());
            } else {
                queryWrapper.orderByAsc(sortData.getProperty());
            }
        }
        return PageData.from(shelfMapper.selectPage(page, queryWrapper));
    }

    @Override
    public int add(Shelf shelf) {
        return shelfMapper.insert(shelf);
    }

    @Override
    public Shelf find(int id) {
        return shelfMapper.selectByPrimaryKey(id);
    }

    @Override
    public int update(Shelf shelf) {
        return shelfMapper.updateByPrimaryKeySelective(shelf);
    }

    @Override
    public int delete(int id) {
        return shelfMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int delete(int userId, int bookId) {
        return shelfMapper.deleteByForeignKey(userId, bookId);
    }

    @Override
    public List<Book> getProfileShelf(Integer usrId) {
        return shelfMapper.getProfileShelf(usrId);
    }

    @Override
    public Shelf select(int userId, int bookId) {
        return shelfMapper.select(userId, bookId);
    }

    // 获取书的收藏量
    @Override
    public int shelfNum(int bookId) {
        return shelfMapper.shelfNum(bookId);
    }
}
